.module entry_code 
.pseg 
.org 0 
.global _int0 
        jmp __start 
.global _int1 
        jmp __handler1 
.global _int2 
        jmp __handler2 
.global _int3 
        jmp __handler3
.global _int4
	irmovl $0, %edx
	rmmovl %edx, -1(%eax)
	jmp _loop2

__start:
	irmovl $000fffff,%esp 			; init esp
        call _main
__here: 
        halt
__handler1: 
        ret 
__handler2: 
        halt 
__handler3: 
        halt
_main:
	pushl %ebp
	rrmovl %esp, %ebp
	pushl %ebx
	irmovl $20, %ebx
	subl %ebx, %esp
	irmovl $3, %ecx			; init x
	irmovl $50, %edx		; init player_y
	irmovl $1, %eax			; init bool_x
	rmmovl %ecx, 16(%esp)
	rmmovl %edx, 12(%esp)
	rmmovl %eax, 8(%esp)
	irmovl $117, %ecx		; init y
	irmovl $1, %eax			; init bool_y
	rmmovl %ecx, 4(%esp)
	rmmovl %eax, 0(%esp)
	call _loop

_loop:
	pushl %ebp
	rrmovl %esp, %ebp
	pushl %ebx
	jmp _loop2

_loop2:
	mrmovl 24(%ebp), %ecx
	mrmovl 20(%ebp), %edx
	mrmovl 16(%ebp), %eax
	irmovl $20, %ebx
	subl %ebx, %esp
	rmmovl %eax, 8(%esp)
	rmmovl %edx, 4(%esp)
	rmmovl %ecx, 0(%esp)
	call _moveX
	addl %ebx, %esp
	rmmovl %ecx, 24(%ebp)
	rmmovl %eax, 16(%ebp)
	mrmovl 12(%ebp), %ecx
	mrmovl 8(%ebp), %eax
	subl %ebx, %esp
	rmmovl %eax, 8(%esp)
	rmmovl %edx, 4(%esp)
	rmmovl %ecx, 0(%esp)
	call _moveX
	addl %ebx, %esp
	rmmovl %ecx, 12(%ebp)
	rmmovl %eax, 8(%ebp)
	rrmovl %ecx, %eax
	mrmovl 24(%ebp), %ecx
	call _makesnapshot
	jmp _int4
	popl %ebx
	rrmovl %ebp, %esp
	popl %ebp
	ret
	
_makesnapshot:	
	pushl %ebp
	rrmovl %esp, %ebp	
	pushl %ebx
	pushl %edx
	xorl %esi, %esi
	rrmovl %ecx, %edx

	irmovl $4, %ebx
	shll %ebx, %ecx	
	addl %ecx, %esi
	rrmovl %edx, %ecx

	irmovl $6, %ebx
	shll %ebx, %ecx
	addl %ecx, %esi
	rrmovl %edx, %ecx
	
	irmovl $7, %ebx
	shll %ebx, %ecx
	addl %ecx, %esi
	rrmovl %edx, %ecx
	
	irmovl $8, %ebx
	shll %ebx, %ecx
	addl %ecx, %esi
	rrmovl %edx, %ecx
	
	irmovl $9, %ebx
	shll %ebx, %ecx
	addl %ecx, %esi
	rrmovl %edx, %ecx
	
	irmovl $A, %ebx
	shll %ebx, %ecx
	addl %ecx, %esi
	rrmovl %esi, %ecx	
	
	irmovl $2, %ebx
	shll %ebx, %eax
	addl %eax, %ecx
	irmovl $001fffff, %ebx
	irmovl $000f4240, %edx
	subl %edx, %ebx
	addl %ebx, %ecx
	irmovl $00ffffff, %edx	
	rmmovl %edx, 0(%ecx)
	rrmovl %ecx, %eax
	popl %edx
	popl %ebx
	rrmovl %ebp, %esp
	popl %ebp
	ret
	

_moveX:
	pushl %ebp
	rrmovl %esp, %ebp
	pushl %ebx
	mrmovl 16(%ebp), %eax		;bool_x
	mrmovl 12(%ebp), %edx		;player_y
	mrmovl 8(%ebp), %ecx		;x
	irmovl $6, %ebx
	subl %ebx, %esp
	irmovl $1fb, %ebx
	rmmovl %ebx, 0(%esp)
	call _callinvert
	irmovl $5, %ebx
	rmmovl %ebx, 0(%esp)
	call _callinvert4
	irmovl $6, %ebx
	addl %ebx, %esp
	irmovl $1, %ebx
	addl %eax, %ebx
	irmovl $5, %ebx
	jl _moveX2	
	jmp _moveX3

_moveX2:
	addl %ebx, %ecx
	jmp _moveXfinish

_moveX3:
	subl %ebx, %ecx
	jmp _moveXfinish

_moveXfinish:
	popl %ebx
	rrmovl %ebp, %esp
	popl %ebp
	ret
	
_callinvert:
	pushl %ebp
	rrmovl %esp, %ebp
	pushl %ebx
	mrmovl 8(%ebp), %ebx
	subl %ecx, %ebx
	jl _callinvert2
	jmp _callinvert3

_callinvert4:
	pushl %ebp
	rrmovl %esp, %ebp
	pushl %ebx
	mrmovl 8(%ebp), %ebx
	subl %ecx, %ebx
	jg _callinvert2
	jmp _callinvert3

_callinvert2:
	call _invertbool
	jmp _callinvert3

_callinvert3:
	popl %ebx
	rrmovl %ebp, %esp
	popl %ebp
	ret

_invertbool:
	pushl %ebp
	rrmovl %esp, %ebp
	notl %eax
	rrmovl %ebp, %esp
	popl %ebp
	ret
	

_putcolor:	
	pushl %ebp
	rrmovl %esp, %ebp
	pushl %eax
	pushl %edx
	mrmovl 12(%ebp), %edx
	mrmovl 8(%ebp), %eax
	rmmovl %eax, 0(%edx)	
	popl %edx
	popl %eax
	rrmovl %ebp, %esp
	popl %ebp
	ret
	
.end  
